package GUI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ZhuanPanel extends JPanel implements ActionListener {
    private ImageIcon imageIcon = Util.getIcon("/img/buttonOff.jpg");
    private ImageIcon imageIcon1 = Util.getIcon("/img/buttonSure.jpg");
    private JButton loginjButton;
    private Text2 frame;
    private JButton loginjButton1;
    private JTextField jTextField;
    private JTextField jTextField1;

    public ZhuanPanel(Text2 frame) {
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);

        JLabel jLabel = new JLabel("转账人:");
        jLabel.setBounds(200, 150, 150, 50);
        jLabel.setFont(new Font("宋体", Font.BOLD, 30));
        this.add(jLabel);

        JLabel jLabel2 = new JLabel("金额:");
        jLabel2.setBounds(200, 200, 150, 50);
        jLabel2.setFont(new Font("宋体", Font.BOLD, 30));
        this.add(jLabel2);

        jTextField = new JTextField();
        jTextField.setBounds(350, 150, 200, 30);
        this.add(jTextField);

        jTextField1 = new JTextField();
        jTextField1.setBounds(350, 200, 200, 30);
        this.add(jTextField1);
        //转账
        loginjButton1 = new JButton(imageIcon1);
        loginjButton1.setBounds(0, 350, 100, 50);
        this.add(loginjButton1);
        loginjButton1.addActionListener(this);
        //退出
        loginjButton = new JButton(imageIcon);
        loginjButton.setBounds(690, 350, 100, 50);
        this.add(loginjButton);
        loginjButton.addActionListener(this);
    }

    public void zhuan() {
        String a=jTextField.getText();
        double b=Double.valueOf(jTextField1.getText());
        for(int i=0;i<frame.getList().size();i++){
            if(a.equals(frame.getList().get(i).getAccount())&&a!=frame.getLoginUser().getAccount()){
                if(b>frame.getLoginUser().getBalance()){
                    JOptionPane.showMessageDialog(this,"余额不足");
                }else if(b<frame.getLoginUser().getBalance()){
                    frame.getLoginUser().setBalance(frame.getLoginUser().getBalance()-b);
                    JOptionPane.showMessageDialog(this,"转账成功");
                }
            }
            JOptionPane.showMessageDialog(this,"账号输入错误");
            break;
        }

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == loginjButton) {
            frame.switchPanel("menu");
        }else if(e.getSource()==loginjButton1){
            frame.getZhuanPanel().zhuan();
        }
    }
}
